﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IntroduceCompany.Singleton;
using IntroduceCompany.Util;

namespace IntroduceCompany.Controllers
{
    [HandleError]
    [IntroduceCompany.Models.CheckModule(ConstantClass.Backend_CompanyInformation_MenuID)]
    public class CompanyInformationController : BaseController
    {
        //
        // GET: /CompanyInformation/

        [Authorize(Roles = ConstantClass.autCompanyInformation)]
        public ActionResult CompanyInformationEdit(String comID, String languageID)
        {

            if (languageID == null || languageID == "")
            {
                languageID = ConstantClass.mainLanguageID;
            }

            if (comID == null || comID == "")
            {
                comID = "1";
            }

            CompanyInformationView comInfoView = DataSingleton.GetCompanyInformationModels().GetCompanyInformationView(languageID);

            if (comInfoView == null)
            {
                comInfoView = new CompanyInformationView();
                CompanyInformation temCompany = DataSingleton.GetCompanyInformationModels().GetCompanyInformationByComID(Convert.ToInt16( comID));
                comInfoView.Phone = temCompany.Phone;
                comInfoView.Fax = temCompany.Fax;
                comInfoView.Email = temCompany.Email;
                comInfoView.FullName = temCompany.Administrator.FullName;
            }
            ViewData["LaguageList"] = new SelectList(DataSingleton.GetLanguageModels().GetLanguageList().ToList(), "LanguageID", "LanguageName", languageID);

            return View("/Views/Backend/CompanyInformation/CompanyInformationEdit.aspx", comInfoView);
        }


        [Authorize(Roles = ConstantClass.autCompanyInformation)]
        [HttpPost, ValidateInput(false), ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult CompanyInformationEditSubmit(CompanyInformationView company)
        {

            //Check Cookie Language
            string languageID = ConstantClass.mainLanguageID;
            if (Request.Cookies[ConstantClass.cooLanguage] != null && Request.Cookies[ConstantClass.cooLanguage].Value != "")
            {
                languageID = Request.Cookies[ConstantClass.cooLanguage].Value;
            }

            string username = HttpContext.User.Identity.Name;

            //Edit information of company when press Save button. Not Edit when change language
            if (DataSingleton.GetCompanyInformationModels().CheckCompanyInformationLanguageIsExisted(company.ComID, company.LanguageID)  )
            {

                if (ModelState.IsValid && TryUpdateModel(company) && DataSingleton.GetCompanyInformationModels().EditCompanyInformation(company, username))
                {
                    ModelState.Clear();
                    ModelState.AddModelError("NoticePage", "Thông tin công ty đã được cập nhật.");
                }
                else
                {
                    ModelState.AddModelError("NoticePage", "Có lỗi khi cập nhật thông tin công ty. Xin vui lòng thử lại");
                }

                ViewData["LaguageList"] = new SelectList(DataSingleton.GetLanguageModels().GetLanguageList().ToList(), "LanguageID", "LanguageName", company.LanguageID);

                return View("/Views/Backend/CompanyInformation/CompanyInformationEdit.aspx", DataSingleton.GetCompanyInformationModels().GetCompanyInformationView(company.LanguageID));
            }
            else
            {
                //Insert another language for company information

                ViewData["LaguageList"] = new SelectList(DataSingleton.GetLanguageModels().GetLanguageList().ToList(), "LanguageID", "LanguageName", company.LanguageID);

                if (ModelState.IsValid && TryUpdateModel(company) && DataSingleton.GetCompanyInformationModels().InsertCompanyInformationLanguage(company))
                {
                    ModelState.Clear();
                    ModelState.AddModelError("NoticePage", "Thông tin công ty đã được cập nhật thêm ngôn ngữ mới.");

                    return View("/Views/Backend/CompanyInformation/CompanyInformationEdit.aspx", DataSingleton.GetCompanyInformationModels().GetCompanyInformationView(company.LanguageID));
                }
                else
                {
                    ModelState.AddModelError("NoticePage", "Có lỗi khi thêm ngôn ngữ mới cho thông tin công ty. Xin vui lòng thử lại");
                    return View("/Views/Backend/CompanyInformation/CompanyInformationEdit.aspx", company);
                }

            }
            
        }

    }
}
